﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ShenRuQianChuChapt6Binding
{
    /// <summary>
    /// chapt6_3_8UseAdoNETObjcecASBindingSourceListView.xaml 的交互逻辑
    /// </summary>
    public partial class chapt6_3_8UseAdoNETObjcecASBindingSourceListView : Window
    {
        public chapt6_3_8UseAdoNETObjcecASBindingSourceListView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = this.Load();           
            //this.listViewStudent.ItemsSource = dt.DefaultView;

            //DataTable对象放在一个对象的DataContext属性
            this.listViewStudent.DataContext = dt;
            this.listViewStudent.SetBinding(ListView.ItemsSourceProperty, new Binding());
        }

        //返回一个数据表对象
        private DataTable Load()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "Tim";
            dr[2] = "29";
            dt.Rows.Add(dr);

            DataRow dr1 = dt.NewRow();
            dr1[0] = 2;
            dr1[1] = "Tom";
            dr1[2] = "28";
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr2[0] = 3;
            dr2[1] = "Tony";
            dr2[2] = "27";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3[0] = 4;
            dr3[1] = "Kyle";
            dr3[2] = "26";
            dt.Rows.Add(dr3);

            DataRow dr4 = dt.NewRow();
            dr4[0] = 5;
            dr4[1] = "Vian";
            dr4[2] = "25";
            dt.Rows.Add(dr4);

            DataRow dr5 = dt.NewRow();
            dr5[0] = 6;
            dr5[1] = "Emily";
            dr5[2] = "24";
            dt.Rows.Add(dr5);

            return dt;
        }
    }
}
